//
//  CityTourListVC.m
//  ImGuider
//
//  Created by 李陆涛 on 2017/12/26.
//  Copyright © 2017年 imguider. All rights reserved.
//

#import "CityTourListVC.h"
#import "LineNodeCCell.h"
#import "LinePreDetailVC.h"
#import "PlayerItem.h"
@interface CityTourListVC () <UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;

@property (nonatomic, strong) NSArray *dataSource;

@end

@implementation CityTourListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(self.isPopular){
        self.title = MYLocalizedString(@"HOME_HOT_RECORD", nil);
    }else{
        self.title = MYLocalizedString(@"HOMEPAGE_CITYTOUR", nil);
    }
    [self.collectionView registerNib:[UINib nibWithNibName:@"LineNodeCCell" bundle:nil] forCellWithReuseIdentifier:@"LineNodeCCell"];
    
    self.view.backgroundColor = kBGColor;
    self.collectionView.backgroundColor = kBGColor;
    [self setupData];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    if (self.isItemAction) {//是否需要隐藏  playItem  排除第一次一直可以循环点的bug
        
        [[PlayerItem playItemShareManager] hide];
    }
}

- (void)viewWillDisappear:(BOOL)animated {
    
    [super viewWillDisappear:animated];
    if (self.isItemAction) {
        [[PlayerItem playItemShareManager] show];
    }
}

- (void)setupData {
    
    [ActivityIndicatorView showHUDAddedTo:self.view];
    if (!self.city) {
        
        [NetAPIManager getMoreLinesWithBlock:^(NSArray<LineModel *> *linesArray, NSError *error) {
            
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            self.dataSource = linesArray;
            [self.collectionView reloadData];
        }];
    }else {
        
        [ActivityIndicatorView showHUDAddedTo:self.view];
        [NetAPIManager getAllLinesBy:self.city.cid start:@(0) count:@(0) block:^(NSArray *linesArray, NSError *error) {
            [ActivityIndicatorView hideHUDAddedTo:self.view];
            self.dataSource = [NSMutableArray arrayWithArray:linesArray];
            [self.collectionView reloadData];
        }];
    }
}


#pragma mark  --  UICollectionViewDelegate, UICollectionViewDataSource


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    return self.dataSource.count;
}
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    LineModel *model = self.dataSource[indexPath.row];
    
    if (!model.country) {
        model.country = self.city.country;
    }
    
    LineNodeCCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LineNodeCCell" forIndexPath:indexPath];
    cell.model = model;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    [collectionView deselectItemAtIndexPath:indexPath animated:NO];
    
    LineModel * model = self.dataSource[indexPath.row];
    
    LinePreDetailVC *detailVC = [IGUtil loadViewController:@"LinePreDetailVC" storyboard:@"LineDetail"];
    detailVC.lineid = model.lineid;
    detailVC.hidesBottomBarWhenPushed = YES;

    
    [self.navigationController pushViewController:detailVC animated:YES];
    
    [MobClick event:@"2_1_2"];
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CGFloat width = kScreenWidth - 32;
    CGFloat height = width * 9 / 16 + 72;
    
    return CGSizeMake(width, height);
}

@end

